//
//  PhotoAppView.swift
//  SwiftUILean
//
//  Created by wyknmjj on 2024/5/13.
//

import SwiftUI


struct PhotoAppView:View {
    var coordinator: PhotoCoordinator = .init()
    var body: some View {
        NavigationStack{
            PhotoAppHome()
                .environment(coordinator)
        }
        .overlay{
            Rectangle()
                .fill(.background)
                .ignoresSafeArea()
                .opacity(coordinator.animateView ? 1 - coordinator.dragProgress : 0)
        }
        .overlay {
            if (coordinator.selectedItem != nil) {
                PhotoDetail()
                    .environment(coordinator)
                    .allowsHitTesting(coordinator.showDetailView)
            }
        }.overlayPreferenceValue(PhotoHeroKey.self){ value in
            if let selectedItem=coordinator.selectedItem,
               let sAncor=value[selectedItem.id+"SOURCE"],
               let dAncor=value[selectedItem.id+"DEST"]{
                PhotoHeroLayer(
                    item: selectedItem,
                    sAnchor: sAncor,
                    dAnchor: dAncor)
                .environment(coordinator)
            }
        }
    }
}


#Preview {
    PhotoAppView()
}
